<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<f:view>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <link rel="stylesheet" type="text/css" href="css/dataTable.css" />
            <link rel="stylesheet" type="text/css" href="css/messages.css" />
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>Simple Forum</title>
        </head>
        <body>  
            <h:form id="headerLinks">
                <h:commandLink action="topics" value="[back to topics]" />
                <h:commandLink action="login" value="[logout]" style="padding-left : 55px;" />
            </h:form>
            
            <h2><h:outputText value="\"#{forumPostController.topic.title}\"" /></h2>
            
            <h3>Add post</h3>
            <h:form id="addPost">
                <h:panelGrid border="1" columns="2">
                    <h:outputLabel for="text" value="text:" />
                    <h:panelGroup>
                        <h:inputText id="text" value="#{forumPostController.selected.content}" required="true" label="#{msg.text_label}" >
                            <f:validator validatorId="DuplicatePostValidator" />
                        </h:inputText>
                        <h:message for="text" infoClass="infoMessages" errorClass="errorMessages" />
                    </h:panelGroup>
                    <h:panelGroup>&nbsp;</h:panelGroup>
                    <h:panelGroup>
                        <h:commandButton value="Add Post from #{user.username}" action="#{forumPostController.create}" >
                            <f:setPropertyActionListener target="#{forumPostController.selected.username}" value="#{user}"/>
                        </h:commandButton>
                    </h:panelGroup>
                </h:panelGrid>
            </h:form>

            <h3>Posts</h3>

            <h:outputText value="There is no posts there"  rendered="#{forumPostController.items.rowCount == 0}" />

            <h:panelGroup id="postsPanel" layout="block" rendered="#{forumPostController.items.rowCount > 0}">
                <h:form id="postsByAuthor">

                    <h:outputText value="#{forumPostController.pagination.pageFirstItem + 1}..#{forumPostController.pagination.pageLastItem + 1}/#{forumPostController.pagination.itemsCount}"/>&nbsp;
                    <h:commandLink action="#{forumPostController.previous}" value="#{bundle.Previous} #{forumPostController.pagination.pageSize}" rendered="#{forumPostController.pagination.hasPreviousPage}"/>&nbsp;
                    <h:commandLink action="#{forumPostController.next}" value="#{bundle.Next} #{forumPostController.pagination.pageSize}" rendered="#{forumPostController.pagination.hasNextPage}"/>&nbsp;


                    <h:dataTable var="post" value="#{forumPostController.items}" rendered="#{forumPostController.items.rowCount > 0}" >
                        <%-- styleClass="postsTable"
                               headerClass="headerAlignment"
                               columnClasses="numberColumn,userColumn,textColumn,dateColumn,removeColumn"
                               rowClasses="oddRow,evenRow" --%> 

                        <h:column>

                            <h:outputText value="On "/>
                            <h:outputText value="#{post.postDate}">
                                <f:convertDateTime pattern="#{msg.date_format}"/>
                            </h:outputText>

                            <h:commandButton action="#{forumPostController.destroyAndView}" image="images/removePost.gif" style="padding-left: 15px;"
                                rendered="#{user.role == 'admin' || user.username == post.username.username}">
                                <f:setPropertyActionListener target="#{forumPostController.selected}" value="#{post}"/>
                            </h:commandButton>

                            <br/>
                            <h:outputText value="#{post.username.username} wrote:"/>

                            <h:panelGroup id="postInfoPanel" layout="block" style="padding: 5px; background-color: #C6E2FF;">
                                <h:outputText  value="#{post.content}" style="padding: 5px"/>
                            </h:panelGroup>
                        </h:column>
                        <%--
                        <h:column>
                                <f:facet name="header"><h:outputText value="Author"/></f:facet>
                                <h:commandLink>
                                        <f:setPropertyActionListener target="#{author}" value="#{post.username}"/>
                                        <h:outputText value="#{post.username.username}"/>
                                </h:commandLink>
                        </h:column>
                        <h:column>
                                <f:facet name="header"><h:outputText value="Text"/></f:facet>
                                <h:outputText value="#{post.content}"/>
                        </h:column>
                        <h:column>
                                <f:facet name="header"><h:outputText value="Date"/></f:facet>
                                <h:outputText value="#{post.postDate}">
                                        <f:convertDateTime pattern="#{msg.date_format}"/>
                                </h:outputText>
                        </h:column>  
                        <h:column>
                                <h:commandButton action="#{removePostRequest.removePost}" image="images/removePost.gif">
                                        <f:setPropertyActionListener target="#{removePostRequest.post}" value="#{post}"/>
                                </h:commandButton>
                        </h:column> --%>
                    </h:dataTable>
                </h:form>
            </h:panelGroup>


        </body>
    </html>
</f:view>